﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Downloader
{
    public static class InvokeExtension
    {
        public delegate void ButtonDelegate(Button button, bool b);
        private delegate void UpdateStatusDelegate(Label label, String content);
        private delegate void UpdateProgressBarDelegate(ProgressBar progressBar);
        public static void IncreaseStep(this ProgressBar progressBar)
        {
            if (progressBar.InvokeRequired)
            {
                var a = new UpdateProgressBarDelegate(IncreaseStep);
                progressBar.Invoke(a, new object[] { progressBar });
            }
            else
            {
                progressBar.PerformStep();
            }
        }

        /// <summary>
        /// Updates the status text.
        /// </summary>
        /// <param name="label"></param>
        /// <param name="content">The content.</param>
        public static void UpdateStatusText(this Label label, string content)
        {
            if (label.InvokeRequired)
            {
                var a = new UpdateStatusDelegate(UpdateStatusText);
                label.Invoke(a, new object[] { label, content });
            }
            else
            {
                label.Text = content;
            }
        }

        /// <summary>
        /// Sets the enabled.
        /// </summary>
        /// <param name="button">The button.</param>
        /// <param name="b">if set to <c>true</c> [b].</param>
        public static void SetEnabled(this Button button, bool b)
        {
            if (button.InvokeRequired)
            {
                var a = new ButtonDelegate(SetEnabled);
                button.Invoke(a, new object[] { button, b });
            }
            else
            {
                button.Enabled = b;
            }
        }
    }
}
